/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-12-01     RT-Thread    TIM1 test sample
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

/* 外部HAL函数声明 */
extern uint32_t HAL_RCC_GetPCLK2Freq(void);

#define HWTIMER_DEV_NAME   "timer1"

static rt_device_t hw_dev = RT_NULL;
static rt_hwtimer_mode_t mode;
static rt_tick_t last_tick = 0;
static rt_uint32_t callback_count = 0;

static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
    rt_tick_t current_tick = rt_tick_get();
    rt_tick_t delta = 0;
    
    if (last_tick != 0)
    {
        delta = current_tick - last_tick;
        rt_kprintf("timer1 timeout! tick: %d, delta: %d ms, count: %d\n", 
                   current_tick, delta, ++callback_count);
    }
    else
    {
        rt_kprintf("timer1 first timeout! tick: %d\n", current_tick);
        callback_count = 1;
    }
    
    last_tick = current_tick;
    
    return RT_EOK;
}

static int timer1_sample(int argc, char *argv[])
{
    rt_err_t ret = RT_EOK;
    rt_hwtimerval_t timeout_s;
    rt_uint32_t freq = 1000000; /* 1MHz，计数频率 */
    rt_uint32_t pclk2_freq;

    /* 重置统计变量 */
    last_tick = 0;
    callback_count = 0;

    /* 查找定时器设备 */
    hw_dev = rt_device_find(HWTIMER_DEV_NAME);
    if (hw_dev == RT_NULL)
    {
        rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
        return -RT_ERROR;
    }

    /* 以读写方式打开设备 */
    ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
    if (ret != RT_EOK)
    {
        rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
        return ret;
    }

    /* 打印时钟信息用于诊断 */
    pclk2_freq = HAL_RCC_GetPCLK2Freq();
    rt_kprintf("PCLK2: %d Hz, Timer clock: %d Hz\n", pclk2_freq, pclk2_freq * 2);
    rt_kprintf("Prescaler will be: %d\n", (pclk2_freq * 2 + freq / 2) / freq - 1);
    
    /* 设置超时回调函数 */
    rt_device_set_rx_indicate(hw_dev, timeout_cb);

    /* 设置计数频率为1MHz */
    rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
    rt_kprintf("Set frequency to: %d Hz\n", freq);
    
    /* 设置定时器模式为周期性定时器 */
    mode = HWTIMER_MODE_PERIOD;
    ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
    if (ret != RT_EOK)
    {
        rt_kprintf("set mode failed! ret is :%d\n", ret);
        return ret;
    }

    /* 设置定时器超时值为1s并启动定时器 */
    timeout_s.sec = 1;      /* 秒 */
    timeout_s.usec = 4000;     /* 微秒 */

    if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
    {
        rt_kprintf("set timeout value failed\n");
        return -RT_ERROR;
    }

    return ret;
}
MSH_CMD_EXPORT(timer1_sample, timer1 sample);

static int timer1_stop(void)
{
    if (hw_dev == RT_NULL)
    {
        rt_kprintf("please run timer1_sample first!\n");
        return -RT_ERROR;
    }

    rt_device_close(hw_dev);
    hw_dev = RT_NULL;
    
    rt_kprintf("timer1 stopped!\n");
    
    return RT_EOK;
}
MSH_CMD_EXPORT(timer1_stop, stop timer1);
